﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint.Linq;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;

namespace MS.InformationManagement.BLL.DeliveryForms
{
    public class DeleteWindowReceptionCommand : IBaseCommand
    {

        #region Private Methods

        EntitySet<EnvíoDeCorrespondencia> _deliveries;

        #endregion

        #region Constructors

        public DeleteWindowReceptionCommand(EntitySet<EnvíoDeCorrespondencia> deliveries)
        {
            _deliveries = deliveries;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            using(var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foreach (var entity in _deliveries)
                {
                    var windowReceptionEntity = (from item in dc.DocumentsRecibidosVentanilla
                                                 where item.IdDocumento == entity.IdDocumento &&
                                                       item.BibliotecaDelDocumento == entity.BibliotecaDelDocumento
                                                 select item).FirstOrDefault();
                    if(windowReceptionEntity != null)
                        dc.DocumentsRecibidosVentanilla.DeleteOnSubmit(windowReceptionEntity);
                }
                dc.SubmitChanges();
            }
        }

        public void Undo()
        {
            //TODO: Undo transaction
        }

        #endregion
    }
}
